<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/


header("Content-type: text/html; charset=utf-8");  
header("Cache-Control: no-cache");

$EXCMS = "work";

error_reporting (0);

include $_SERVER['DOCUMENT_ROOT']."/engine/settings.php";
include $_SERVER['DOCUMENT_ROOT']."/engine/mysql.class.php";
$db = new DB($server, $dbusername, $dbpassword, $dbname);
$db->connect();

if (!$mainsettings = $db->query ("SELECT * FROM site_main")) { print "<center><b>Невозможно взять данные из БД!<br>Обратитесь к администратору сайта!</b>"; exit; }
$SITE = $db->fetch_array ($mainsettings);
$theme = $SITE['site_theme'];
if (isset($_COOKIE['login']) && isset ($_COOKIE['pass']) && $_COOKIE['login'] != "false" && $_COOKIE['pass'] != "false") {
	$GLOBAL_USER_LOGIN = addslashes($_COOKIE['login']);
	$GLOBAL_USER_PASS = addslashes($_COOKIE['pass']);
	if ($GLOBAL_USER_LOGIN != "" && $GLOBAL_USER_PASS != "")
		$SELECT_USER = $db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_login = '$GLOBAL_USER_LOGIN' && user_password = '$GLOBAL_USER_PASS'");
	if ($db->num_rows ($SELECT_USER) == 1) {
		$GLOBAL_USER = $db->fetch_array ($SELECT_USER);
	}
}

if (!isset($GLOBAL_USER)) exit;
if (!isset($_POST['blog_id']) || !isset($_POST['type'])) exit;

$id = intval($_POST['blog_id']);
$get_com = $db->query ("SELECT cat_users FROM ".DB_PEREFIX."_art_category WHERE cat_id = '$id'");
if ($db->num_rows($get_com) != 1) exit;

$com = $db->fetch_array ($get_com);

$user_com = $GLOBAL_USER['user_blogs'];

if ($_POST['type'] == "join") {
	if ($user_com == "") $new = $id;
	else $new = "$user_com,$id";
	$db->query ("UPDATE ".DB_PEREFIX."_users SET user_blogs = '$new' WHERE user_id = '{$GLOBAL_USER['user_id']}'");
	$db->query ("UPDATE ".DB_PEREFIX."_art_category SET cat_users = '".($com['cat_users']+1)."' WHERE cat_id = '$id'");
	
	$opt = "";
	$file = file_get_contents ($_SERVER['DOCUMENT_ROOT']."/themes/$theme/elements.tpl");
	preg_match ("#{exit_blog}(.+?){/exit_blog}#is", $file, $element);
	$opt .= str_replace ("{ID}", $id, $element[1]);
	print $opt;
}
elseif ($_POST['type'] == "exit" && in_blog ($id)) {
	$comm = explode (",", $user_com);
	if (count($comm) == 1) $new = "";
	else {
		if ($comm[0] == $id) $new = substr ($user_com, strlen($com[0]), strlen($user_com)-1);
		else {
			$new_ = explode (",$id", $user_com);
			$new = $new_[0].$new[1];
		}
	}
	$db->query ("UPDATE ".DB_PEREFIX."_users SET user_blogs = '$new' WHERE user_id = '{$GLOBAL_USER['user_id']}'");
	$db->query ("UPDATE ".DB_PEREFIX."_art_category SET cat_users = '".(intval($com['cat_users'])-1)."' WHERE cat_id = '$id'");
	
	$opt = "";
	$file = file_get_contents ($_SERVER['DOCUMENT_ROOT']."/themes/$theme/elements.tpl");
	preg_match ("#{join_blog}(.+?){/join_blog}#is", $file, $element);
	$opt .= str_replace ("{ID}", $id, $element[1]);
	print $opt;
	
}
else exit;

function in_blog ($id) {
	global $GLOBAL_USER;
	$com = explode (",", $GLOBAL_USER['user_blogs']);
	foreach ($com as $c) if ($c == $id) return true;
	return false;
}
